<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="90" label-position="top">
            <el-form-item label="不良品项" prop="status">
                <sc-form-table ref="table" v-model="rejects" :addTemplate="addTemplate" placeholder="暂无数据" height="300">
                    <el-table-column prop="rejects_id" label="不良品项" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.rejects_id" style="width: 100%" clearable filterable placeholder="请选择不良品项">
                                <el-option v-for="item in rejectsList" :key="item.id" :label="item.rejects_name" :value="item.id" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column prop="rejects_num" label="不良品数" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.rejects_num" placeholder="不良品数" clearable></el-input>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column prop="reasion" label="不良原因" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.reasion" placeholder="不良原因" clearable></el-input>
                        </template>
                    </el-table-column> -->
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			dialog: {
				client: false
			},
			mode: "add",
			titleMap: {
				add: '新增合同',
				edit: '不良品项',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			form: {
                rejects: []
			},
            addTemplate: {},
            rejectsList: [],
            index: ""
		}
	},
	mounted() {
        this.getRejects()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
        submit(){
            let array = []
            this.rejects.map(item=>{
                array.push({
                    rejects_id : item.rejects_id,
                    rejects_num : item.rejects_num,
                    reasion : ""
                })
            })
            this.form.rejects = array
            this.$emit('success', this.form,this.index)
            this.visible = false;
        },
		//表单注入数据
		async setData(data) {
            this.index = data.index
            this.rejects = data.row.rejects_all
		},
        // 不良品项
        async getRejects(){
            var res = await this.$API.craft.rejects.all.get();
            this.rejectsList = res.data;
        },
	}
}
</script>

<style lang="scss">
.el-input-group__append{
    padding: 0 8px;
}
</style>
